﻿/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : club_my.js                                 
*  Description  : 회원가입 
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-05-04  이윤노    Created.
*  2012-05-04  이윤노    Complete.
*******************************************************************************/

jQuery(function($){
		
	// 검색타입
	function clsSearchType()
	{
		this._HTMLDom = null;
		this.idx = null;
		
		this._init();
	}
	
	clsSearchType.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content div.golf_board article.golf_board_search div.selectlayer:eq(0)");
			
			this._HTMLDom = {
				"$layer": $layer
			}
			this.idx = 0;
			
			this._bind();
		}
		, _bind: function(){
			var thisClass = this;
			var $layer = this._HTMLDom["$layer"];
			
			$layer.find("p:eq(0)").click(function(event){
				event.preventDefault();
				
				if($layer.find("ul").css("display")=="block") $layer.find("ul").hide(300);
				else $layer.find("ul").show(300);
			});
			
			$layer.find("li").click(function(event){
				event.preventDefault();
				
				thisClass.setIdx($layer.find("li").index($(this)));
			});
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.find("p:eq(0) a").html($layer.find("li a").eq(this.idx).html());
		}
		, setIdx: function(idx){
			this.idx = idx;
			this.display();
			this._HTMLDom["$layer"].find("ul").hide(300);
		}
		, $getSelect: function(){
			return this._HTMLDom["$layer"].find("ul");
		}
		, getValue: function(){
			return this.idx;
		}
	}
	
	// 검색어
	function clsSearchText()
	{
		this._HTMLDom = null;
		this._ERRMSG = {
			"_NOVALUE": "검색어를 입력해 주십시요."
		}
		this.errMsg = null;
		
		this._init();
	}
	
	clsSearchText.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content div.golf_board article.golf_board_search :text");
			this._HTMLDom = {
				"$layer": $layer
			}
		}
		, getValue: function(){
			return this._HTMLDom["$layer"].val();
		}
		, setValue: function(val){
			this._HTMLDom["$layer"].val(val);
		}
		, isValid: function(){
			this.errMsg = null;
			
			if(this.getValue() == ""){
				this.errMsg = this._ERRMSG["_NOVALUE"];
				return false;
			}
			
			return true;
		}
		, getError: function(){
			return this.errMsg;
		}
		, focus: function(){
			this._HTMLDom["$layer"].trigger("focus");
		}		
	}	
	
	//검색정렬
	function clsSearchSort(){
		this._HTMLDom = null;
		this.sKey = null;
		this.sOrder = null;
		this._init();	
	}
	
	clsSearchSort.prototype = {
		_init: function(){
			var $layer = $("section#main_content div.club_home article.list02 thead th");
			this._HTMLDom = {
				"$layer": $layer	
				, "$sorts": $layer.find("span")
			}			
			
			this.sKey = null; //개설일기본
			this.sOrder = null; //내림차순기본
		}	
		, $getBtnSort: function(){
			var $layer = this._HTMLDom["$layer"];
			var $sorts = this._HTMLDom["$sorts"];
			
			return $sorts;
		}
		, setIdx: function(val){
			if(this.sKey == val){
				this.sOrder = !this.sOrder;	
			}else {
				this.sKey = val;
				this.sOrder = true;
			}
			
			var $sorts = this._HTMLDom["$sorts"];
			
			$sorts.removeClass("th_t").eq(this.sKey).addClass("th_t");
		}
		, getSort: function(){
			return this.sOrder;
			//return this._HTMLDom["$layer"].data("sort");
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"]; 
			var $sorts = this._HTMLDom["$sorts"];
			var idx = $layer.data("idx");
			
			$sorts.removeClass("th_t");
			
			// 현재 인덱싱된 타이틀만 on설정
			for(var i=0;i<$sorts.length;++i){
				if(idx==i) $sorts.eq(i).addClass("th_t");	
			}			
		}		
	}
	
	//검색카테고리
	function clsSearchCategory(){
		this._HTMLDom = null;
		this.categoryList = null;
		
		this.idx = null;
		
		this._init();
	}
	
	clsSearchCategory.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content div.golf_board article.golf_board_search div.selectlayer:eq(1)");
			
			this._HTMLDom = {
				"$layer": $layer
				, "$element": $layer.find("li:first")
			}			
						
			this.idx=0;
			$layer.find("ul").empty();	
			
		}
		, setList: function(thisData){
			this.categoryList = thisData;
			this.display();	
		}
		, display: function(){
			var thisClass= this;
			var $layer = this._HTMLDom["$layer"];
			var $element = this._HTMLDom["$element"];

			$layer.find("ul").empty();
			
			for(var i=0;i<this.categoryList.length;++i){
				$layer.find("ul").append($element.clone(true));
			}
			
			$layer.find("li").each(function(n){			

				$(this).find("a").data("카테고리키",thisClass.categoryList[n]["카테고리키"]).html(thisClass.categoryList[n]["카테고리명"]).click(function(event){
					thisClass.setIdx(n);
				});
			});
			
			this._bind();
		}
			
		, _bind: function(){
			var thisClass = this;
			var $layer = this._HTMLDom["$layer"];
			
			$layer.find("p:eq(0)").click(function(event){
				event.preventDefault();
				
				if($layer.find("ul").css("display")=="block") {
					$layer.find("ul").hide(300);
				}else {
					$layer.find("ul").show(300);
				}
			});
			
		}		
		, setIdx: function(idx){
			this.idx = idx;
			var $layer = this._HTMLDom["$layer"];			
			
			$layer.find("p:eq(0) a").html($layer.find("ul a").eq(idx).html());
			$layer.find("ul").hide(300);
		}
		, $getSelect: function(){
			return this._HTMLDom["$layer"].find("ul");
		}
		, getValue: function(){
			return this._HTMLDom["$layer"].find("ul a").eq(this.idx).data("카테고리키");
		}
	}
	
	// 동호회 카테고리 리스트
	function clsClubListCategory()
	{
		this._HTMLDom = null;
		this.listData = null;
		
		this._init();
	}
	
	clsClubListCategory.prototype = {
		_init: function(){
			var $layer = $("section#main_content div.club_home article.list02 tbody");
			this._HTMLDom = {
				"$layer": $layer
				, "$element": $layer.find("tr:first").clone(true)
				, "$empty": $("body").find("article.board_list_none:eq(0)")
			}
			
			var head = $("section#main_content div.club_home article.list02 thead");
			head.find("th:eq(2)").css("cursor", "pointer");
			head.find("th:eq(3)").css("cursor", "pointer");
			head.find("th:eq(4)").css("cursor", "pointer");
			$layer.empty();
		}
		, setList: function(thisData){
			this.listData = thisData;
			this.display();
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			var $element = this._HTMLDom["$element"];
			var thisClass = this;
			
			$layer.empty();
			this.isNoData();
			
			for(var i=0;i<this.listData.length;i++){
				$layer.append($element.clone(true));
			}
							
			// 데이터링
			$layer.find("tr").each(function(n){
				$(this).find("td:eq(0)").html(thisClass.listData[n]["카테고리명"]);
				$(this).find("td:eq(1) strong").html(thisClass.listData[n]["동호회명"]);
				$(this).find("td:eq(1)").contents().filter(function(){
					return (this.nodeType == 3);
				}).after(thisClass.listData[n]["동호회설명"]).remove();
				$(this).find("td:eq(1)").click(function(){
					document.location.href = "../self_club/home.html?club_seq="+ thisClass.listData[n]["동호회키"];
				});
				$(this).find("td:eq(2) strong").html(thisClass.getFormatNumber(thisClass.listData[n]["회원수"]));
				$(this).find("td:eq(3)").html(thisClass.getFormatNumber(thisClass.listData[n]["게시글"]));
				$(this).find("td:eq(4)").html(thisClass.getFormatDateYmd(thisClass.listData[n]["개설일"],". "));
			});
		}
		, spinStart: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.hide();
			if($layer.parent().parent().prev("#tmpSpin").length == 0){
				$layer.parent().parent().before("<div id='tmpSpin' style='position:absolute; left:350px; top:200px;'></div>");
			}
			$layer.parent().parent().prev("#tmpSpin").spin();
		}
		, spinEnd: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.parent().parent().prev("#tmpSpin").spin(false);
			$layer.show();
		}
		, getFormatDateYmd: function (n,v){			
			n = String(n);
			
			return n.substr(0,4) +v+ n.substr(4,2) +v+ n.substr(6,2);
		}
		, getFormatNumber: function(n){
			n = String(n);			
			i = n.length;
			r = "";
			while(true){				
				if(i<=3) {
					r = n.substr(0,i) + r;					
					break;
				}
				r = ","+ n.substr(i-3,3) + r;				
				i-=3;
			}
			return r;
		}
		, isNoData: function(){
			if(!this.listData.length) {
				this._HTMLDom["$empty"].show();
				$("div#idCategoryClubListPage").hide();
			}else {
				this._HTMLDom["$empty"].hide();
				$("div#idCategoryClubListPage").show();
			}
		}
	}
	
	
	
		
	// 서버통신
	function clsComm()
	{
		//초기화
		this.category = null;
		
		this.categoryClubList = null;
		this.categoryClubPageData = null;
		this.categoryClubRequest = null;
		
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){
//------------------------ 카테고리구분------------------------//			
			this.category = {					
					"카테고리키": null
					, "카테고리명": null
			}	
//------------------------ 카테고리구분------------------------//			
//------------------------ 카테고리별동호회------------------------//		
			this.categoryClubList = [
				{
					"키": "001"
					, "카테고리키":"서울"
					, "동호회명": "안산 시흥 화성 여성모임"
					, "인사말":"여자들만 칩니다. 강미모인들은 모여주세요~ 비밀번호는 동호.."
					, "회원수": "123 명"
					, "게시글": "345"
					, "개설일": "2012.05.23"
				}
			];
			
			this.categoryClubPageData = {
				"전체수":null
			}
			
			this.categoryClubRequest = {
				"카테고리키": null
				, "페이지": null
				, "검색어":null
				, "검색종류":null
				, "검색카테고리":null
				, "정렬키":null
				, "정렬방향": null
			}
//------------------------ 카테고리별동호회------------------------//		
		}		
//------------------------ 카테고리구분------------------------//		
		, getCategory: function(){
			var thisClass = this;
			
			// ajax 시작			
			//parameter setting and run
			_gf.ajax({
				"sid":"CL001",
				"servicename":"getCategoryList"
			}, {
				
			}, function(ret){
				
				// callback and mapping
				thisClass.category = [];
				
				if(ret["msp"]["contents"]["items"]){
					if(!ret["msp"]["contents"]["items"].length){
						ret["msp"]["contents"]["items"] = new Array(ret["msp"]["contents"]["items"]);
					}
					for(var i=0;i<ret["msp"]["contents"]["items"].length;i++){
						thisClass.category.push({
							"카테고리키": ret["msp"]["contents"]["items"][i]["categoryId"]
							, "카테고리명": ret["msp"]["contents"]["items"][i]["categoryName"]
						});
					}
				}
				
				$(thisClass).trigger("evtFinishCategory");
			});
			// ajax 끝
		}
		, getCategoryData: function(){
			return this.category;
		}
//------------------------ 카테고리구분------------------------//		
//------------------------ 카테고리별동호회------------------------//		
		, getCategoryClubList: function(){
			var thisClass = this;
			
			var szPage = this.categoryClubRequest["페이지"];	
			
			var szTitle = "";
			var szContent =  "";
			var szNickName = "";
			
			var szSortKey = this.categoryClubRequest["정렬키"];
			var szSortOption = this.categoryClubRequest["정렬방향"];
			
			var szSearchType = this.categoryClubRequest["검색종류"];
			var szSearchText = this.categoryClubRequest["검색어"];
			var szSearchCategory = this.categoryClubRequest["검색카테고리"];
			
			switch(this.categoryClubRequest["검색종류"]){
				case 0:  szTitle = szContent = szNickName = this.categoryClubRequest["검색어"]; break; //전체
				case 1:  szTitle = this.categoryClubRequest["검색어"]; break;	//제목
				case 2:  szContent =  this.categoryClubRequest["검색어"]; break;//제목+내용
				case 3:  szNickName	= ""; break;//작성자
				default : break;
			}
			
			// ajax 시작			 
			//parameter setting and run
			_gf.ajax({
				"sid":"CL001",
				"servicename":"searchClub",
				"listcount":10,
				"currpageno":szPage
			}, {
				 "searchOption": szSearchType
				, "searchValue": szSearchText
				, "categoryId" : szSearchCategory
				, "sortBy" : szSortKey
				, "sortOption": szSortOption
			}, function(ret){
				
				thisClass.categoryClubPageData = {
					"전체수": ret["msp"]["contents"]["@totalcount"]
				}
				
				// callback and mapping
				thisClass.categoryClubList = [];
			
				if(ret["msp"]["contents"]["items"]){
					if(!ret["msp"]["contents"]["items"].length){
						ret["msp"]["contents"]["items"]	= new Array(ret["msp"]["contents"]["items"]);
					}
					for(var i=0;i<ret["msp"]["contents"]["items"].length;i++){
						thisClass.categoryClubList.push({
							 "카테고리명": ret["msp"]["contents"]["items"][i]["clubCategoryMaster"]["categoryName"]
							, "동호회키": ret["msp"]["contents"]["items"][i]["clubSeq"]
							, "동호회명": ret["msp"]["contents"]["items"][i]["clubName"]
							, "회원수": ret["msp"]["contents"]["items"][i]["memberCnt"]							
							, "게시글": ret["msp"]["contents"]["items"][i]["newBoardCnt"]							
							, "개설일": ret["msp"]["contents"]["items"][i]["regDate"]
							, "동호회설명": ret["msp"]["contents"]["items"][i]["clubDesc"]
							
						});
					}
				}
				
				$(thisClass).trigger("evtFinishCategoryClubList");
			},true);
			// ajax 끝
			
		}
		, getCategoryClubData: function(){
			return this.categoryClubList;
		}
		, getCategoryClubPageData: function(){
			return this.categoryClubPageData;
		}
		, setCategoryClubRequest: function(key, val){
			this.categoryClubRequest[key] = val;
		}
		, getCategoryClubRequest: function(key){
			return this.categoryClubRequest[key];
		}
//------------------------ 카테고리별동호회------------------------//
	}
	
	// 초기화면 제어
	function clsControl()
	{
		
		//초기화
		this.objComm = null;
		
		this.objClubCategory = null;
		
		this.objSearchType = null;
		this.objSearchText = null;		
		this.objSearchCategory = null;
		
		this._init();
	}
	
	clsControl.prototype = {
				
		//초기화
		_init: function(){
			
			this.objComm = new clsComm();
			
			this.objSearchType = new clsSearchType();
			this.objSearchText = new clsSearchText();
			this.objSearchCategory = new clsSearchCategory();
			this.objSearchSort = new clsSearchSort();
			
			this.objClubListCategory = new clsClubListCategory();
			this.objClubListCategoryPage = new _gf.makePaging($("#idCategoryClubListPage"), 10, 10);
			
			this._bind();
			
			//카테고리구분 조회
			this.objComm.getCategory();
				
		}
		
		//데이터 화면처리
		, _bind: function(){
			var thisClass = this;
			
			// 카테고리구분 조회 완료이벤트			
			$(this.objComm).bind("evtFinishCategory", function(){
				var thisData = this.getCategoryData();
								
				thisClass.objSearchCategory.setList(thisData);
				
				//검색조건 파라미터 설정
				//thisClass.objSearchSort.setIdx(0);
				thisClass.objSearchType.setIdx(document.objUtil.getParam("q_type"));
				thisClass.objSearchText.setValue(document.objUtil.getParam("q_text"));
				thisClass.objSearchCategory.setIdx(document.objUtil.getParam("q_cate"));
				
				thisClass.objComm.setCategoryClubRequest("검색종류",thisClass.objSearchType.getValue());
				thisClass.objComm.setCategoryClubRequest("검색어",thisClass.objSearchText.getValue());
				thisClass.objComm.setCategoryClubRequest("검색카테고리",thisClass.objSearchCategory.getValue());
				
				//정렬조건 설정
				thisClass.objSearchSort.setIdx(2);
				thisClass.objComm.setCategoryClubRequest("정렬키",2);
				thisClass.objComm.setCategoryClubRequest("정렬방향",1);
				
				//페이징 설정
				thisClass.objComm.setCategoryClubRequest("페이지",1);			
				thisClass.objClubListCategoryPage.setPage(1);
				
				//동호회카테고리 조회
				thisClass.objClubListCategory.spinStart();						
				thisClass.objComm.getCategoryClubList();
			
			});
			
			// 카테고리목록 조회 완료이벤트
			$(this.objComm).bind("evtFinishCategoryClubList", function(){
				var thisData = this.getCategoryClubData();
				var pageData = thisClass.objComm.getCategoryClubPageData();
				
				thisClass.objClubListCategory.spinEnd();
				
				thisClass.objClubListCategory.setList(thisData);
				
				thisClass.objClubListCategoryPage.setTotalCount(pageData["전체수"]);
				thisClass.objClubListCategoryPage.display();
			});
			// 카테고리목록 페이지이동 이벤트
			$(this.objClubListCategoryPage).bind("evtPageClick", function(){
				var thisPage = this.getPage();
				
				thisClass.objComm.setCategoryClubRequest("페이지", thisPage);
				thisClass.objClubListCategory.spinStart();
				thisClass.objComm.getCategoryClubList();
			});
			
			//전체동호회 버튼 이벤트
			$("div#container section#main_content div.club_home article.depth2_tab ul img[alt=전체동호회]").click(function(event){
				event.preventDefault();					
				document.location.href = "../club/club.html";
			});
			
			//나의동호회 버튼 이벤트
			$("div#container section#main_content div.club_home article.depth2_tab ul img[alt=나의동호회]").click(function(event){
				event.preventDefault();					
				document.location.href = "../club/club_join.html";
			});
			
			// 검색버튼 이벤트
			$("article.golf_board_search :image").click(function(event){
				event.preventDefault();
				/* 검색어 제한
				if(!thisClass.objSearchText.isValid() & thisClass.objSearchType.getValue() != 0){
					alert(thisClass.objSearchText.getError());
					thisClass.objSearchText.focus();
					return;
				}
				*/
				thisClass.objComm.setCategoryClubRequest("검색종류", thisClass.objSearchType.getValue());
				thisClass.objComm.setCategoryClubRequest("검색어", thisClass.objSearchText.getValue());
				thisClass.objComm.setCategoryClubRequest("검색카테고리", thisClass.objSearchCategory.getValue());
				thisClass.objComm.setCategoryClubRequest("페이지", 1);
				thisClass.objComm.setCategoryClubRequest("정렬", 0);
				
				thisClass.objClubListCategoryPage.setPage(1);
				thisClass.objClubListCategory.spinStart();
				thisClass.objComm.getCategoryClubList();
								
			});
			
			//정렬버튼 이벤트
			this.objSearchSort.$getBtnSort().click(function(event){
				var idx = thisClass.objSearchSort.$getBtnSort().index($(this));
				thisClass.objSearchSort.setIdx(idx);
				
				var sorts = thisClass.objSearchSort.getSort();				
				
				thisClass.objComm.setCategoryClubRequest("페이지", 1);
								
				thisClass.objComm.setCategoryClubRequest("정렬키", idx);
				thisClass.objComm.setCategoryClubRequest("정렬방향", (sorts?"2":"1"));
				
				thisClass.objClubListCategoryPage.setPage(1);
				thisClass.objClubListCategory.spinStart();
				thisClass.objComm.getCategoryClubList();
				
				//thisClass.objSearchText.setValue("정렬키="+idx+",순차="+(sorts?"2":"1"));
			});
			
			//동호회개설 버튼 이벤트
			$(".btn_depth a img").click(function(event){
					event.preventDefault();					
					document.location.href = "../club/club_creat.html";
			});
		}
					
	}
	
	document.objControl = new clsControl();
});